//
//  SPCMSServiceConfig.m
//  BaseProject
//
//  Created by Sailer Guo on 2019/12/5.
//  Copyright © 2019 Base. All rights reserved.
//

#import "SPCMSService.h"
#import "SPCMSServiceConfig.h"

@interface SPCMSService ()

@property (nonatomic,strong) SPCMSServiceConfig *cmsServiceConfig;

@end

@implementation SPCMSService

- (NSString *)getH5PageFor:(SPH5Page)h5Page {
    NSString *url = [self.cmsServiceConfig.h5PageMap objectForKey:@(h5Page)];
    return  url != nil ? url : @"";
}

- (NSString *)getOrderDetailUrl:(NSString *)orderNo {

    NSString *orderDetail = @"html/orderDetail.html";
//    NSString * requestUrl = [NSString stringWithFormat:@"%@h5/%@%@?onm=%@",H5BaseUrl,[HttpManager h5_version],RESULT_DETAIL_ORDER, self.tradeNo];

    NSDictionary *dict = @{@"onm":orderNo};
    NSString *url = [self.cmsServiceConfig.baseUrl stringByAppendingFormat:@"h5/%@%@?%@",[self h5Version],orderDetail,[self webUrlForParameters:dict]];
    return url;
}

- (NSString *)webUrlForParameters:(NSDictionary *)parameters {
    NSMutableString *paramsStr = [NSMutableString string];
    for (NSString *key in parameters.allKeys) {
        NSString *keyValue = [key stringByAppendingFormat:@"=%@",parameters[key]];
        if (paramsStr.length > 0) {
            [paramsStr appendFormat:@"&%@",keyValue];
        } else {
            [paramsStr appendString:keyValue];
        }
    }
    return paramsStr;
}

- (NSString *)h5Version {
    NSString *version = @"1.2.0";
    version = [version stringByReplacingOccurrencesOfString:@"." withString:@"_"];
    version = [NSString stringWithFormat:@"v%@/",version];
    return version;
}

- (void)switchEnv:(EnvironmentType)env {
    self.cmsServiceConfig = (SPCMSServiceConfig *)[SPCMSServiceConfig serviceConfigWith:env];
}


@end
